<?php

namespace App\Admin\Controllers;

use App\Models\product;
use App\Http\Controllers\Controller;
use App\Models\Video;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Tools;
use Encore\Admin\Grid\Tools\BatchActions;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class VideoController extends Controller
{
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function index(Content $content)
	{
		return $content->header('视频管理')->description('视频列表')->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function show($id, Content $content)
	{
		return $content->header('查看视频')->description('详情')->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function edit($id, Content $content)
	{
		return $content->header('编辑视频')->description('编辑')->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function create(Content $content)
	{
		return $content->header('创建视频')->description('创建视频')->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid()
	{
		$grid = new Grid(new Video);

		$grid->id('Id');
		$grid->description('描述');
		$grid->is_release('是否发布')->radio([
			'0' => '不发布',
			'1' => '发布'
		]);
		$grid->created_at('创建时间');
		$grid->updated_at('编辑时间');

		$grid->tools(function(Tools $tools) use ($grid) {
			$tools->batch(function(BatchActions $batch) use ($grid) {
				$batch->disableDelete();
			});
		});

		$grid->actions(function(Grid\Displayers\Actions $actions) {
			$actions->disableDelete();
		});

		$grid->model()->orderBy('is_release', 'desc');
		$grid->disableExport();
		$grid->disableRowSelector();
		$grid->disableFilter();

		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 *
	 * @return Show
	 */
	protected function detail($id)
	{
		$show = new Show(Video::findOrFail($id));

		$show->id('Id');
		$show->description('描述');
		$show->url('视频')->videl();
		$show->is_release('是否发布')->using([
			'0' => '未发布',
			'1' => '已发布'
		]);
		$show->created_at('创建时间');
		$show->updated_at('编辑时间');
		$show->panel()->tools(function(Show\Tools $tools) {
			$tools->disableDelete();
		});

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form()
	{
		$form = new Form(new Video);

		$form->text('description', '描述')->required();
		$form->largefile('url', '视频');
		$form->image('image', '视频封面')->required();

		$form->radio('is_release', '是否发布')->options([
			'0' => '不发布',
			'1' => '发布'
		])->default('0');
		return $form;
	}
}
